"""
Game -- data structure for holding a Go game.

Ken Harris <kengruven@gmail.com>
2006.9.1
"""

import gobject

from cursor import Cursor

class Game(gobject.GObject):
    __slots__ = ['head', 'cursor']
    __gsignals__={
        'node_added': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
        'node_deleted': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
        'node_edited': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)), # TOADD: attr_name?
        'cursor_moved': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
        }

    def __init__(self, root_node):
        self.__gobject_init__() # this is how you __init__ a GObject subclass

        self.head = root_node
        self.cursor = Cursor(self)


import gtk
black_stone = gtk.gdk.pixbuf_new_from_file("Resources/black-stone.png")
white_stone = gtk.gdk.pixbuf_new_from_file("Resources/white-stone.png")
empty_board = gtk.gdk.pixbuf_new_from_file("Resources/tiny-board.png")

class Node(object):
    ## BACKWARDS COMPATIBILITY: just the raw sgf, for now
    __slots__ = ['properties', 'children', 'parent', 'number']

    def __init__(self):
        # elements: properties (dict of str:str), children (list of Node), parent (Node, or None)
        self.properties = {}
        self.children = []
        self.parent = None
        self.number = 0

    def get_icon(self):
        if 'B' in self.properties:
            return black_stone
        elif 'W' in self.properties:
            return white_stone
        elif self.parent == None:
             return empty_board
        else:
            return None

    def get_text(self):
        if 'C' in self.properties:
            comment = self.properties["C"]
#           max_chars_shown = 20
#           if len(comment) > max_chars_shown:
#               comment = ' - %s...' % comment[:max_chars_shown]
#           else:
#               comment = ' - %s' % comment
            comment = ' - %s' % comment.replace('\n',' ').replace('\r',' ')
        else:
            comment = ''

        if 'B' in self.properties and self.properties['B']:
            move = self.properties["B"]
            text = "%d. %s%s" % (self.number, move, comment)
        elif 'W' in self.properties and self.properties['W']:
            move = self.properties["W"]
            text = "%d. %s%s" % (self.number, move, comment)

        elif 'B' in self.properties or 'W' in self.properties:
            text = "%d. Pass%s" % (self.number, comment)

        elif self.parent == None:
            h = 0
            if 'HA' in self.properties:
                h = int(self.properties["HA"]) # ASSUMES it's int-able.
            if h > 0:
                text = "%d stones" % h
            else:
                text = "Empty board"
        else:
            # (it's a node with no move -- e.g., "here's the score", with only TW/TB/C tags)
            text = u"%d. \N{EM DASH}" % self.number
        return text
